//
//  TLLoveController.m
//  Books
//
//  Created by tao故乡的云 on 2017/3/24.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "TLLoveController.h"
#import "TLChengYu.h"
#import "TLReadPage.h"

@interface TLLoveController ()
@property(strong,nonatomic) NSArray *loves;
@end

@implementation TLLoveController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.loves.count == 0) {
        // 提示没有还没有收藏，创建前往推荐阅读按钮
        UIView *tips = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 21)];
        label.text = @"您还没有收藏内容...";
        label.textAlignment = NSTextAlignmentCenter;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 21, self.view.bounds.size.width, 20)];
        [btn addTarget:self action:@selector(goRead) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:@"前往阅读" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [tips addSubview:label];
        [tips addSubview:btn];
        [self.view addSubview:tips];
        self.tableView.separatorColor = [UIColor clearColor];
    }
    self.loves = nil;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *)loves {
    if (_loves == nil) {
        NSString *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *path = [cache stringByAppendingPathComponent:@"loves.plist"];
        _loves = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    }
    
    return _loves;
}

- (void)goRead {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    TLReadPage *read = [[TLReadPage alloc] init];
    // 随机数据
    NSMutableArray *items = [NSMutableArray array];
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *path = [cachePath stringByAppendingPathComponent:@"cyLexicon.plist"];
    NSArray *data = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; // 词库
    for (NSInteger i = 0; i < 20; i++) {
        NSInteger a = arc4random_uniform((int)(data.count - 1));
        NSInteger b = arc4random_uniform((int)([data[a] count] - 1));
        [items addObject:data[a][b]];
    }
    read.items = items;
    read.currentItem = 0;
    [self.navigationController pushViewController:read animated:YES];

}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.loves count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.textColor = [UIColor orangeColor];
    }
    TLChengYu *cy = self.loves[indexPath.row];
    cell.textLabel.text = cy.name;
    cell.detailTextLabel.text = cy.pinYin;
    
    return cell;
}

#pragma mark - Table view data delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TLReadPage *read = [[TLReadPage alloc] init];
    read.items = self.loves;
    read.currentItem = indexPath.row;
    read.type = TLReadPageTypeLove;
    [self.navigationController pushViewController:read animated:YES];
}

@end
